<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">                     
            <el-form-item label="消费者名称:" prop="name">
                <el-input
                v-model="queryParams.name"
                placeholder="请输入查询名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
                />
            </el-form-item>


            <el-form-item label="标签:" prop="tags">
                <el-input 
                v-model="queryParams.tags"
                placeholder="请输入标签名称"
                clearable
                size="small"
                @key.enter.native="handleQuery"
                ></el-input>
            </el-form-item>

            
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>

        </el-form>

         <!-- 表工具lan 新增删除等 -->
        <el-row :gutter="20" class="mb8">
            <el-col :span="1.5">
                <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['kong:consumer:add']"                
                >新增</el-button>
            </el-col>

            <el-col :span="1.5">
                    <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    :loading="exportLoading"
                    @click="handleExport"
                    v-hasPermi="['kong:consumer:export']"
                >导出</el-button>
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

        </el-row>



        <el-table v-loading="loading" :data="consumers">
            <el-table-column label="消费者" align="center" prop="username"></el-table-column>
            <el-table-column label="消费者id" align="center" prop="custom_id"></el-table-column>
            <el-table-column label="id" align="center" prop="id"></el-table-column>
            <el-table-column label="标签" align="center" prop="tags">
                <template slot-scope="scope">
                    {{scope.row.tags}}
                </template>
            </el-table-column>            
            <el-table-column label="创建时间" align="center" prop="created_at" :formatter="dateFormat"></el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button
                        size="small"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['kong:consumer:edit']"
                    >编辑</el-button>

                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['kong:consumer:remove']"
                    >删除</el-button>

                </template>
            </el-table-column>
        </el-table>

        <el-dialog v-if="addOpen" :visible.sync="addOpen" append-to-body width="680px" >
            <add-consumer @callback="handleCloseAddDialog"/>
        </el-dialog>

        <el-dialog v-if="editOpen" :visible.sync="editOpen" append-to-body width="70%">
            <edit-consumer :consumerEntity="consumerEntity"/>
        </el-dialog>

    </div>
</template>
<script>
import moment from 'moment'
import {ConsumerList,deleteConsumer} from "@/api/kong/consumer"
import addConsumer from './addConsumer.vue';
import EditConsumer from './editConsumer.vue';
export default {
  components: { addConsumer, EditConsumer },
    data(){
        return{
            consumers:[],
            loading: false,
            // 总条数
            total: 0,
            // table 加载标识
            loading: false,
            // 搜索显示标识
            showSearch: true,
            // 查询条件
            queryParams:{
                pageNum: 1,
                pageSize: 10,
            },
            single: false,
            multiple: false,
            exportLoading: false,
            showSearch:true,
            addOpen: false,
            editOpen: false,
            consumerEntity: null,
        }
    },
    created(){
        this.getList();

    },
    methods: {
        getList(){
            ConsumerList().then(res=>{
                // console.log(JSON.stringify(res))
                this.consumers=res.rows;
            }).catch(err=>{})
        },
        handleQuery(row){},
        resetQuery(){},
        handleAdd(){
            this.addOpen=true;
        },
        handleCloseAddDialog(){
            this.addOpen=false;
            this.getList();
        },
        handleUpdate(row){
            this.consumerEntity=row;
            this.editOpen=true;
        },
        handleDelete(row){
            this.$confirm('确定要删除吗？','警告',{
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function(){
                return deleteConsumer(row.id);
            }).then(res=>{
                this.getList();
                this.msgSuccess("删除成功");
            })
        },
        handleExport(){},
        //时间处理函数
        dateFormat(row, column){
            const _date = row[column.property]*1000; 
            if(_date ==undefined){return ''};
            return moment(_date).format("YYYY-MM-DD HH:mm:ss");
        },
        dateFormatLong(_date){
            if(_date ==undefined){return ''};
            return moment(_date*1000).format("YYYY-MM-DD HH:mm:ss");
        },
    }
    
}
</script>
<style scoped>

</style>